/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.web.common.util.request;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;

import java.util.Map;

/**
 * 这个包存在的意义只是演示另一种请求的方式
 *
 * @see ApiRequestUtil
 * @author yoara
 */
public class ApiServiceRequest {
    //请求类型
    private HttpMethod method;
    private String uri;
    private Map<String, Object> urlVariables;
    private Map<String, Object> parameterMap;

    private ApiServiceRequest(HttpMethod method, String uri, Map<String, Object> parameterMap) {
        this.method = method;
        this.uri = uri;
        this.parameterMap = parameterMap;
    }

    /**
     * @param uri          api请求地址
     * @param parameterMap 参数map,Json封装
     * @return
     */
    public static ApiServiceRequest makePostRequest(String uri, Map<String,Object> parameterMap) {
        if (StringUtils.isEmpty(uri)) {
            throw new InitRequestException("错误的uri地址");
        }
        if (parameterMap == null || parameterMap.size() == 0) {
            throw new InitRequestException("请输入有效参数");
        }
        ApiServiceRequest request =
                new ApiServiceRequest(HttpMethod.POST, uri, parameterMap);
        return request;
    }

    /**
     * @param uri          api请求地址
     * @param parameterMap 参数map
     * @return
     */
    public static ApiServiceRequest makeGetRequest(String uri, Map parameterMap) {
        if (StringUtils.isEmpty(uri)) {
            throw new InitRequestException("错误的uri地址");
        }
        ApiServiceRequest request =
                new ApiServiceRequest(HttpMethod.GET, uri, parameterMap);
        return request;
    }

    public HttpMethod getMethod() {
        return method;
    }

    public String getUri() {
        return uri;
    }

    public Map<String, Object> getParameterMap() {
        return parameterMap;
    }

    public String parameterMapToUriTailString() {
        if (parameterMap == null || parameterMap.size() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer("?");
        for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue().toString()).append("&");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }

    public Map<String, Object> getUrlVariables() {
        return urlVariables;
    }

    /**
     * 设置路径替换参数
     * @param urlVariables
     */
    public void setUrlVariables(Map<String, Object> urlVariables) {
        this.urlVariables = urlVariables;
    }
}
